﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.shop
{
    public partial class userbaseinfo : MxWeiXinPF.Web.UI.UserPage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        public string carturl = "";
        private MxWeiXinPF.BLL.wx_dt_user_BaseData daladd = new MxWeiXinPF.BLL.wx_dt_user_BaseData();
        private int id = 0;
        public string indexurl = "";
        public string userinfourl = "";
        public string categoryurl = "";

        protected void btnHome_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", base.openid }));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (base.openid.Equals(""))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"请在微信中打开页面\");</script>");
            }
            else if (this.txtBirthday.Text.Trim().Equals(""))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"生日不能为空\");</script>");
            }
            else if (!this.hid.Value.Trim().Equals(""))
            {
                if (!this.DoEdit(int.Parse(this.hid.Value.Trim())))
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存过程中发生错误\");</script>");
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"修改成功\");</script>");
                }
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存过程中发生错误,请联系系统管理员\");</script>");
            }
        }

        private bool DoEdit(int _id)
        {
            try
            {
                string email = Utils.DropHTML(this.txtEmail.Text);
                string str2 = Utils.DropHTML(this.txtNickName.Text);
                string avatar = Utils.DropHTML(this.txtAvatar.Text);
                string selectedValue = this.rblSex.SelectedValue;
                string datestr = this.txtBirthday.Text.Trim();
                DateTime birthday = DateTime.Now;
                if (!string.IsNullOrEmpty(datestr))
                {
                    if (!DateTime.TryParse(datestr, out birthday))
                    {
                        birthday = DateTime.Now;
                    }
                }
                string mobile = Utils.DropHTML(this.txtMobile.Text.Trim());
                string address = Utils.DropHTML(this.txtAddress.Text.Trim());
                string truename = Utils.DropHTML(this.txttruename.Text.Trim());
                string jobtype = Utils.DropHTML(this.txtjobtype.Text.Trim());
                bool flag = false;
                MxWeiXinPF.BLL.users users = new MxWeiXinPF.BLL.users();
                if (users.UpdateUserBaseInfo(base.openid, email, str2, avatar, selectedValue, birthday, mobile, address, truename, jobtype, _id))
                {
                    flag = true;
                }
                return flag;
            }
            catch (Exception ex)
            {
                
            }
            return false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                base.wid = MyCommFun.RequestInt("wid");
                base.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                if (!this.Page.IsPostBack)
                {

                    this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                    if (!string.IsNullOrEmpty(base.openid))
                    {
                        this.ShowInfo(base.openid);
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }

        private void ShowInfo(string openid)
        {
            MxWeiXinPF.Model.users modelByOpenid = new MxWeiXinPF.BLL.users().GetModelByOpenid(openid);
            this.txtEmail.Text = modelByOpenid.email;
            this.txtNickName.Text = modelByOpenid.nick_name;
            this.txtAvatar.Text = modelByOpenid.avatar;
            this.rblSex.SelectedValue = modelByOpenid.sex;
            DateTime birthday = modelByOpenid.birthday;
            this.txtBirthday.Text = modelByOpenid.birthday.ToString("yyyy-M-d");
            this.txtMobile.Text = modelByOpenid.mobile;
            this.txtAddress.Text = modelByOpenid.address;
            this.txttruename.Text = modelByOpenid.truename;
            this.txtjobtype.Text = modelByOpenid.jobtype;
            this.hid.Value = modelByOpenid.id.ToString();
            this.id = modelByOpenid.id;
        }
    }
}